﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Authorize;
using Project_KhuyenMaiVang.Models;
using Project_KhuyenMaiVang.DAO;

namespace Project_KhuyenMaiVang.Controllers
{
    [MyAuthorize]
    public class DoanhNghiepController : Controller
    {
        public ActionResult Index()
        {
           AccountDAO accountDao=new AccountDAO();
            AccountDTO accountDto = accountDao.getAccount(User.Identity.Name);
            Session["ID"] = accountDto.IDAccount;
            return View();
        }

        public ActionResult DanhSachDonDatHang()
        {
            AccountDAO accountDAO = new AccountDAO();
            int idAccount = accountDAO.GetIDAccountFrom(User.Identity.Name);

            List<DonDatHang> dsDonDatHang = new List<DonDatHang>();
            DonDatHangDAO ddhDAO = new DonDatHangDAO();
            TinKhuyenMaiDAO tkmDAO = new TinKhuyenMaiDAO();
            TinhTrangDonDatHangDAO ttddHang = new TinhTrangDonDatHangDAO();
            List<TinKhuyenMaiDTO> dsTkmai = tkmDAO.getAllSaleOffNewWasPublishedBy(idAccount);
            for (int i = 0; i < dsTkmai.Count; i++)
            {
                List<DonDatHangDTO> dsDonDatHangTemp = ddhDAO.getAllOrders(dsTkmai[i].MaTinKhuyenMai);
                for (int j = 0; j < dsDonDatHangTemp.Count; j++)
                {
                    string nguoiDatHang = accountDAO.getAccount(dsDonDatHangTemp[j].IDAccount).UserName;
                    string tenSanPham = tkmDAO.getDetailOfSaleOffNews(dsDonDatHangTemp[j].MaTinKhuyenMai).ThongTinTomTat;
                    string tinhTrangDonHang = ttddHang.getOrderState(dsDonDatHangTemp[j].MaTinhTrangDonDatHang);
                    dsDonDatHang.Add(new DonDatHang(nguoiDatHang, tenSanPham, dsDonDatHangTemp[j].SoLuong, tinhTrangDonHang));
                }
            }
            return View(dsDonDatHang);
        }

        public ActionResult DangKyTinKhuyenMai()
        {
            List<SanPhamDTO> listsp = new List<SanPhamDTO>();
            //Lấy idaccount, sẽ đổi lại xài session
            AccountDAO accountDao = new AccountDAO();
            AccountDTO accountDto = accountDao.getAccount(User.Identity.Name);

            SanPhamDAO sanPhamDao = new SanPhamDAO();
            listsp = sanPhamDao.GetListSanPhamByIDAccount(accountDto.IDAccount);

            ViewBag.DSSanPham = listsp;
            LoaiSanPhamDAO loaiSanPhamDao = new LoaiSanPhamDAO();
            List<LoaiSanPhamDTO> ListLoaiSP = loaiSanPhamDao.GetListLoaiSanPhamDTO();
            ViewBag.DsLoaiSanPham = ListLoaiSP;

            HinhThucGiaoHangDAO hinhThucGiaoHangDao=new HinhThucGiaoHangDAO();
            ViewBag.DsHinhThucGiaoHang = hinhThucGiaoHangDao.GetListHinhThucGiaoHang();

            KhuVucDAO khuVucDao=new KhuVucDAO();
            ViewBag.DsKhuVuc = khuVucDao.GetListAreas();


            return View();
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult DangKyTinKhuyenMai(TinKhuyenMaiModel tinkhuyenmai, HttpPostedFileBase filehinh1, HttpPostedFileBase filehinh2,
                                        HttpPostedFileBase filehinh3)
        {
            TinKhuyenMaiModel temp=tinkhuyenmai;            
            AccountDAO accountDao = new AccountDAO();
            AccountDTO accountDto = accountDao.getAccount(User.Identity.Name);
            Session["ID"] = accountDto.IDAccount;
            int idAc = accountDto.IDAccount;
            temp.TinKhuyenMaiDTO.IdAccount = idAc;
            int idsanpham;

            if (tinkhuyenmai.isNhapSanPhamMoi)
            {
                SanPhamDAO sanPhamDao = new SanPhamDAO();
                temp.SanPhamDTO.IDAccount = idAc;
                idsanpham = sanPhamDao.themSanPham(temp.SanPhamDTO);
                if(idsanpham!=-1)
                    temp.TinKhuyenMaiDTO.MaSanPham = idsanpham;
                //Upload hinh
                int idSp = idsanpham;
                HinhAnhDAO hinhAnhDao = new HinhAnhDAO();
                UploadFileHinh(filehinh1, idSp, hinhAnhDao);
                UploadFileHinh(filehinh2, idSp, hinhAnhDao);
                UploadFileHinh(filehinh3, idSp, hinhAnhDao);
            }

            TinKhuyenMaiDAO tinKhuyenMaiDao = new TinKhuyenMaiDAO();
            temp.TinKhuyenMaiDTO.TinhTrang = true;
            tinKhuyenMaiDao.insert(temp.TinKhuyenMaiDTO);
            return RedirectToAction("DangKyTinKhuyenMai");          
        }

        private void UploadFileHinh(HttpPostedFileBase filehinh3, int idSp, HinhAnhDAO hinhAnhDao)
        {
            if (filehinh3 != null)
            {
                if (filehinh3.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(filehinh3.FileName);
                    hinhAnhDao.insertImage(new HinhAnhDTO(idSp, fileName));
                    var path = Path.Combine(Server.MapPath("~/Content/Uploads/" + fileName));
                    filehinh3.SaveAs(path);
                }
            }
        }

        [HttpGet]
        public ActionResult ThongTinCaNhan()
        {
            LienHeDAO lienHeDAO = new LienHeDAO();
            AccountDAO accountDAO = new AccountDAO();
            int idAccount = accountDAO.GetIDAccountFrom(User.Identity.Name);
            DoanhNghiepDAO doanhNghiepDAO = new DoanhNghiepDAO();
            DoanhNghiepDTO doanhNghiepDTO = doanhNghiepDAO.getBussinessInfo(idAccount);

            int idLienHe = doanhNghiepDAO.getBussinessInfo(idAccount).MaLienHe;
            LienHeDTO lienHeDTO = lienHeDAO.getLienHe(idLienHe);

            DoanhNghiepInfo doanhNghiep = new DoanhNghiepInfo();
            doanhNghiep.DiaChi = lienHeDTO.DiaChi;
            doanhNghiep.Email = lienHeDTO.Email;
            doanhNghiep.Facebook = lienHeDTO.Facebook;
            doanhNghiep.SoDienThoai = lienHeDTO.SoDienThoai;
            doanhNghiep.TenDoanhNghiep = doanhNghiepDTO.TenDoanhNghiep;
            doanhNghiep.TrangWeb = doanhNghiepDTO.Web;
            return View(doanhNghiep);
        }

        [HttpPost]
        public ActionResult ThongTinCaNhan(DoanhNghiepInfo doanhNghiep)
        {
            AccountDAO accountDAO = new AccountDAO();
            int idAccount = accountDAO.GetIDAccountFrom(User.Identity.Name);
            
            // update password
            string password = doanhNghiep.newPassword1;
            if (password != null)
            {
                accountDAO.updateAccountInfo(idAccount, password);
            }

            //update info
            DoanhNghiepDAO doanhNghiepDAO = new DoanhNghiepDAO();
            doanhNghiepDAO.updateInfo(idAccount, doanhNghiep.TenDoanhNghiep, doanhNghiep.TrangWeb);

            int idLienHe = doanhNghiepDAO.getBussinessInfo(idAccount).MaLienHe;
            LienHeDAO lienHeDAO = new LienHeDAO();
            lienHeDAO.updateInfo(idLienHe, doanhNghiep.SoDienThoai, doanhNghiep.Email, doanhNghiep.Facebook, doanhNghiep.DiaChi);
            return RedirectToAction("ThongTinCaNhan", "DoanhNghiep");
        }

        public ActionResult DangSanPham()
        {
            LoaiSanPhamDAO loaiSanPhamDao = new LoaiSanPhamDAO();
            List<LoaiSanPhamDTO> ListLoaiSP = loaiSanPhamDao.GetListLoaiSanPhamDTO();
            ViewBag.DsLoaiSanPham = ListLoaiSP;
            return View();
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult DangSanPham(SanPhamDTO sanpham, HttpPostedFileBase filehinh1, HttpPostedFileBase filehinh2,
                                        HttpPostedFileBase filehinh3)
        {
            SanPhamDAO sanPhamDao = new SanPhamDAO();
            sanpham.IDAccount = Int32.Parse(Session["ID"].ToString());
            int idSp = sanPhamDao.themSanPham(sanpham);

            HinhAnhDAO hinhAnhDao = new HinhAnhDAO();            
            UploadFileHinh(filehinh1, idSp, hinhAnhDao);
            UploadFileHinh(filehinh2, idSp, hinhAnhDao);
            UploadFileHinh(filehinh3, idSp, hinhAnhDao);

            LoaiSanPhamDAO loaiSanPhamDao = new LoaiSanPhamDAO();
            List<LoaiSanPhamDTO> ListLoaiSP = loaiSanPhamDao.GetListLoaiSanPhamDTO();
            ViewBag.DsLoaiSanPham = ListLoaiSP;
            return RedirectToAction("DangSanPham");
        }
    }
}
    